from basecase import BaseCase


class Runner(object):
    """测试执行器"""

    def __init__(self, config, suite):
        """
        :param config: 执行环境数据的配置
        :param suite: 测试套件数据
        """
        self.config = config
        self.suite = suite
        self.browser = None
        self.context = None
        self.page = None

    def run(self):
        """执行的入口函数"""
        # 执行测试套件的公共前置步骤证
        if self.suite.get("setup_step"):
            self.run_suite_setup()
        # 执行测试套件中的用例
        self.run_suite()
        # 关闭浏览器
        self.page.close()

    def run_suite_setup(self):
        """执行测试套件的公共前置步骤"""
        suite_setup_steps = self.suite.get("setup_step")
        run = BaseCase(self.config)
        for step in suite_setup_steps:
            print('执行前置步骤', step['desc'])
            run.perform(step)
        # 保存前置步骤创建的浏览器对象
        self.browser = run.browser
        self.context = run.context
        self.page = run.page

    def run_suite(self):
        """执行套件"""
        cases = self.suite.get('cases')
        #         便利套件中所有的用例
        for case in cases:
            print(f'开始执行用例：', {case.get('name')})
            self.run_case(case)

    def run_case(self, case):
        """执行单条用例"""
        #         获取用例的执行步骤
        steps = case.get('steps', [])
        run = BaseCase(self.config, self.browser, self.context, self.page)
        for step in steps:
            #             执行步骤
            print(f'正在执行用例步骤', step.get('desc'))
            run.perform(step)
        #     保存前置步骤创建的浏览器对象
        self.browser = run.browser
        self.context = run.context
        self.page = run.page

if __name__ == '__main__':
    env_config = {
        "is_debug": False,
        "browser_type": "chromium",
        "host": "https://www.ketangpai.com",
        "global_variable": {
            "username": "python166666",
            "password": "123qwe123"
        }
    }
    suite = {
        'id': "编号",
        'name': "登录功能测试",
        # 测试套件的公共前置操作
        'setup_step': [
            {
                "desc": "打开浏览器",
                "method": "open_browser",
                "params": {
                    "browser_type": "chromium"
                }
            },
            # {
            #     "desc": "打开网页",
            #     "method": "open_url",
            #     "params": {
            #         'url': "/#/login"
            #     }
            # },
            # {
            #     "desc": "等待2秒",
            #     "method": "wait_for_time",
            #     "params": {
            #         "timeout": 2000
            #     }
            # },
        ],
        # 用例
        "cases": [
            {
                'id': "编号1",
                'name': "登录密码错误",
                "skip": False,
                "steps": [
                    {
                        "desc": "打开网页",
                        "method": "open_url",
                        "params": {
                            'url': "/#/login"
                        }
                    },
                    {
                        "desc": "等待2秒",
                        "method": "wait_for_time",
                        "params": {
                            "timeout": 2000
                        }
                    },
                    {
                        "desc": "输入账号",
                        "method": "fill_value",
                        "params": {
                            'locator': '//input[@placeholder="请输入邮箱/手机号/账号"]',
                            'value': "${{username}}"
                        }
                    },
                    {
                        "desc": "等待2秒",
                        "method": "wait_for_time",
                        "params": {
                            "timeout": 2000
                        }
                    },
                    {
                        "desc": "输入密码",
                        "method": "fill_value",
                        "params": {
                            'locator': '//input[@placeholder="请输入密码"]',
                            'value': "${{password}}"
                        }
                    },
                    {
                        "desc": "点击登录",
                        "method": "click_role",
                        "params": {
                            'role': 'button',
                            "name": "登录"
                        }
                    },{
                        "desc": "等待2秒",
                        "method": "wait_for_time",
                        "params": {
                            "timeout": 2000
                        }
                    },

                ]
            },
            # {
            #     'id': "编号1",
            #     'name': "登录密码错误",
            #     "skip": False,
            #     "steps": [
            #         {
            #             "desc": "重置浏览器上下文",
            #             "method": "reset_browser_context",
            #             "params": {
            #             }
            #
            #         },
            #
            #         {
            #             "desc": "打开网页",
            #             "method": "open_url",
            #             "params": {
            #                 'url': "/#/login"
            #             }
            #         },
            #
            #         {
            #             "desc": "等待2秒",
            #             "method": "wait_for_time",
            #             "params": {
            #                 "timeout": 2000
            #             }
            #         },
            #         {
            #             "desc": "输入账号",
            #             "method": "fill_value",
            #             "params": {
            #                 'locator': '//input[@placeholder="请输入邮箱/手机号/账号"]',
            #                 'value': "qwerty12345"
            #             }
            #         },
            #         {
            #             "desc": "等待2秒",
            #             "method": "wait_for_time",
            #             "params": {
            #                 "timeout": 2000
            #             }
            #         },
            #         {
            #             "desc": "输入密码",
            #             "method": "fill_value",
            #             "params": {
            #                 'locator': '//input[@placeholder="请输入密码"]',
            #                 'value': "12345234"
            #             }
            #         },
            #         {
            #             "desc": "点击登录",
            #             "method": "fill_value",
            #             "params": {
            #                 'locator': '//input[@placeholder="请输入密码"]',
            #                 'value': "12345234"
            #             }
            #         }
            #
            #     ]
            # }
        ]
    }

    Runner(env_config, suite).run()
